#include "mainwindow.h"
#include "ui_mainwindow.h"




MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  //初始化变量
  ui->setupUi(this);
  Init();
}

void MainWindow::Init()
{
    cmenu=NULL;
    tcl_usb=NULL;
    sortFlag=true;
    ch_is_exist=false;
    is_atv_edit=false;
    is_dtv_edit=false;
    count_col_atv=0;
    count_col_dtv=0;
    cur_show=-1;
    cur_row=-1;
    read_file_name=STD_FILE_FR_TV;
    write_file_name=STD_FILE_FR_PC;
    model_atv=NULL;
    model_dtv=NULL;
    ms_tv=NULL;

    cur_atv_loc=0;
    cur_dtv_loc=0;
    //界面控件初始化
   // ui->an_DelCol->setVisible(false);
   // ui->an_DelRow->setVisible(false);
    //ui->an_save_ATV->setVisible(false);
   // ui->an_ShowDTV->setVisible(false);
    ui->actionConvert_standard_file->setEnabled(true);
    ui->actionConvert_tv_file->setEnabled(false);
    ui->actionOpen_standard_file->setEnabled(false);
    ui->actionWrite_stadard_file->setEnabled(false);

    ui->apB_LastPage->setEnabled(false);
    ui->apB_NextPage->setEnabled(false);
    ui->dpB_LastPage->setEnabled(false);
    ui->dpB_NextPage->setEnabled(false);

    ui->tableView_atv->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView_dtv->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView_atv->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView_dtv->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //最后一列全部填充View
    ui->tableView_atv->horizontalHeader()->setStretchLastSection(true);
    ui->tableView_dtv->horizontalHeader()->setStretchLastSection(true);
    //列表头属性
   // ui->tableView_atv->horizontalHeader()->setStyleSheet("background-color: rgb(135, 206, 235)");
    //ui->tableView_dtv->horizontalHeader()->setStyleSheet("backround-color: rgb(135,206,235");
    ui->centralWidget->setStyleSheet("background-color:rgb(135,206,235)");

    //Open std file before data edit.
     connect(ui->actionOpen_standard_file,SIGNAL(triggered()),this,SLOT(open_file()));
    //write std file after datas edited .
    connect(ui->actionWrite_stadard_file,SIGNAL(triggered()),this,SLOT(Write_File()));
    //clicked tableview header sort
      connect(ui->tableView_atv->horizontalHeader(),SIGNAL(sectionPressed(int)),this,SLOT(sortSlot(int)));
      connect(ui->tableView_dtv->horizontalHeader(),SIGNAL(sectionPressed(int)),this,SLOT(sortSlot(int)));


      connect(ui->actionConvert_standard_file,SIGNAL(triggered()),this,SLOT(Con_std_file()));
      connect(ui->actionConvert_tv_file,SIGNAL(triggered()),this,SLOT(Con_tv_file()));

      connect(ui->tableView_atv,SIGNAL(clicked(QModelIndex)),this,SLOT(set_cur_row(QModelIndex)),Qt::DirectConnection);
      connect(ui->tableView_dtv,SIGNAL(clicked(QModelIndex)),this,SLOT(set_cur_row(QModelIndex)),Qt::DirectConnection);

      ui->tableView_atv->setContextMenuPolicy(Qt::CustomContextMenu);
      connect(ui->tableView_atv,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(Show_Menu(QPoint)),Qt::DirectConnection);

      ui->tableView_dtv->setContextMenuPolicy(Qt::CustomContextMenu);
      connect(ui->tableView_dtv,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(Show_Menu(QPoint)),Qt::DirectConnection);

      connect(ui->apB_NextPage,SIGNAL(clicked()),this,SLOT(NextPage()));
      connect(ui->apB_LastPage,SIGNAL(clicked()),this,SLOT(LastPage()));

      connect(ui->dpB_NextPage,SIGNAL(clicked()),this,SLOT(NextPage()));
      connect(ui->dpB_LastPage,SIGNAL(clicked()),this,SLOT(LastPage()));


      connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(set_cur_show(int)));
}

void MainWindow::Refresh()
{
    model_atv->clear();
    model_dtv->clear();

    tcl_usb->Clear();
    ms_tv->Clear();

}
//this program need atv tvs don't have the same channel number.so as dtv
int MainWindow::Find_List_Row()
{
  switch(cur_show)
  {
  case 0://mtk_atv

  case 1://mtk_dtv

  case 2://mstar_atv
  {
      Q_CHECK_PTR(model_atv);
      int atv_num=model_atv->index(cur_row,0).data(0).toUInt();
      Q_CHECK_PTR(ms_tv);
      //Q_ASSERT(ms_tv->mstar_atv!=NULL);
      for(int i=0;i<MAX_NUMBER_OF_ATV_PROGRAM;i++)
      {         
          if(atv_num==ms_tv->mstar_atv->ATVProgramData[i].misc.u_ChannelNumber)
              return i;
      }
      qDebug("In MainWindow::Find_List_Row look for mstar_atv list num  Error\n");
      break;
  }
  case 3:
  {
      Q_CHECK_PTR(model_dtv);
      ushort dtv_num=model_dtv->index(cur_row,0).data(0).toUInt();
      QString btmp="DTV-B";
      QString ctmp="DTV-C";
      ushort dtv_class;
      QString tmp=model_dtv->index(cur_row,2).data(0).toString();
      if(tmp.compare(btmp)==0)
              dtv_class=66;
      else if(tmp.compare(ctmp)==0)
          dtv_class=67;
      else
          dtv_class=84;
      Q_CHECK_PTR(ms_tv);
      for(uint i=cur_dtv_loc;i<ms_tv->Get_DTV_Count()&&(i-cur_dtv_loc)<SHOW_NUM_DATA;i++)
      {

          if((dtv_num==ms_tv->mstar_dtv.at(i)->ui2_Number)&&
                  (dtv_class==tcl_usb->q_mstar_dtv_tv.at(i)->ui2_class))
              return i;
      }
      qDebug("In MainWindow::Find_List_Row look for mstar_dtv list num Error\n");
      break;
  }
  default:
      qDebug("In MainWindow::Find_List_Row cur_show error");

  }
  return 0;
}

int MainWindow::Find_List_Row(int row)
{
  switch(cur_show)
  {
  case 0://mtk_atv

  case 1://mtk_dtv

  case 2://mstar_atv
  {
      Q_ASSERT(model_atv!=NULL);
      int atv_num=model_atv->index(row,0).data(0).toUInt();
      Q_ASSERT(ms_tv->mstar_atv!=NULL);
      for(int i=0;i<MAX_NUMBER_OF_ATV_PROGRAM;i++)
      {

          if(atv_num==(int)ms_tv->mstar_atv->ATVProgramData[i].misc.u_ChannelNumber)
              return i;
      }
      qDebug("In MainWindow::Find_List_Row look for mstar_atv list num  Error\n");
      break;
  }
  case 3:
  {
      Q_ASSERT(model_dtv!=NULL);
      int dtv_num=model_dtv->index(row,0).data(0).toUInt();
      Q_ASSERT(ms_tv!=NULL);
      for(uint i=0;i<ms_tv->Get_DTV_Count();i++)
      {

          if(dtv_num==(int)ms_tv->mstar_dtv.at(i)->ui2_Number)
              return i;
      }
      qDebug("In MainWindow::Find_List_Row look for mstar_dtv list num Error\n");
      break;
  }
  default:
      qDebug("In MainWindow::Find_List_Row cur_show error");

  }
  return -1;
}

MainWindow::~MainWindow()
{
  delete ui;
  delete tcl_usb;
  tcl_usb=NULL;
  delete model_atv;
  delete model_dtv;
  model_atv=NULL;
  model_dtv=NULL;
  delete ms_tv;
  ms_tv=NULL;
  read_file_name.clear();
  write_file_name.clear();
}

void MainWindow::sortSlot(int column)
{

  if(sortFlag)
  {
      if(0==cur_show||2==cur_show)
      {
        Q_ASSERT(ui->tableView_atv!=NULL);
        ui->tableView_atv->sortByColumn(column,Qt::AscendingOrder);
      }
      else if(1==cur_show||3==cur_show)
      {
          Q_ASSERT(ui->tableView_dtv!=NULL);
          ui->tableView_dtv->sortByColumn(column,Qt::AscendingOrder);
      }
      else
      {
          qDebug("In MainWindow::sortSlot cur_show error\n");
          return ;
      }
      sortFlag=false;
  }
  else
  {
      if(0==cur_show||2==cur_show)
      {
        Q_ASSERT(ui->tableView_atv!=NULL);
        ui->tableView_atv->sortByColumn(column,Qt::AscendingOrder);
      }
      else if(1==cur_show||3==cur_show)
      {
          Q_ASSERT(ui->tableView_dtv!=NULL);
          ui->tableView_dtv->sortByColumn(column,Qt::AscendingOrder);
      }
      else
      {
          qDebug("In MainWindow::sortSlot cur_show error\n");
          return ;
      }
      sortFlag=true;
  }
}

void MainWindow::Read_File()
{
  qDebug("Read File in MainWindows");
  if(tcl_usb)
  {
      cmenu=NULL;
      delete tcl_usb;
      tcl_usb=NULL;
      sortFlag=true;
      ch_is_exist=false;
      is_atv_edit=false;
      is_dtv_edit=false;
      count_col_atv=0;
      count_col_dtv=0;
      cur_show=-1;
      cur_row=-1;
      if(model_atv)
      {
          delete model_atv;
          model_atv=NULL;
      }
      if(model_dtv)
      {
          delete model_dtv;
          model_dtv=NULL;
      }

      cur_atv_loc=0;
      cur_dtv_loc=0;
  }
  tcl_usb=new TCL_USB(read_file_name);
  QFile file(read_file_name);
 // string str_file_name=file_name.toStdString();
  if(!file.open(QFile::ReadOnly))
  {
      qDebug("Open File bin Fialed\n");
      return ;
  }
  QDataStream in(&file);
  Q_ASSERT(tcl_usb!=NULL);
  if(!tcl_usb->is_empty())
  {
      tcl_usb->Clear();
  }

  ch_is_exist=tcl_usb->Read_File(in);
  //The Default:The Tools Show ATV Data.
  if(0==tcl_usb->Get_Solution())
  {
      cur_show=0;
  }else if(1==tcl_usb->Get_Solution())
      cur_show=2;
  else
      qDebug("In MainWindow::Read_File Get_sulotion errorn");

  qDebug("fuck");
  ui->apB_LastPage->setEnabled(true);
  ui->apB_NextPage->setEnabled(true);
  ui->dpB_LastPage->setEnabled(true);
  ui->dpB_NextPage->setEnabled(true);

  if(model_atv)
  {
    model_atv->clear();
  }
  else
  {
      model_atv=new QStandardItemModel;
  }
  if(model_dtv)
  {
   model_dtv->clear();
  }
  else
  {
      model_dtv=new QStandardItemModel;
  }

  if(0==tcl_usb->Get_Solution())
  {      //atv
      if(!head_data.isEmpty())
      head_data.clear();
      head_data.append("ch_val");
      head_data.append("freq");
      head_data.append("ui4_tv_sys");
      head_data.append("nw_flag");
      head_data.append("ps_name");
      model_atv->setColumnCount(head_data.size());
      count_col_atv=head_data.size();
      for(int i=0;i<head_data.size();i++)
      {
          model_atv->setHeaderData(i,Qt::Horizontal,head_data.at(i));
      }
      //dtv
      head_data.clear();
      head_data.append("ch_val");
      head_data.append("name");
      head_data.append("class");
      head_data.append("favorite");
      head_data.append("skip");
      Q_CHECK_PTR(model_dtv);
      model_dtv->setColumnCount(head_data.size());
      count_col_dtv=head_data.size();
      for(int i=0;i<head_data.size();i++)
          model_dtv->setHeaderData(i,Qt::Horizontal,head_data.at(i));
  }else{
      if(!head_data.isEmpty())
          head_data.clear();
      head_data.append("ch_val");
      head_data.append("name");
      head_data.append("class");
      head_data.append("favorite");
      head_data.append("skip");
      model_atv->setColumnCount(head_data.size());
      count_col_atv=head_data.size();
      for(int i=0;i<head_data.size();i++)
            model_atv->setHeaderData(i,Qt::Horizontal,head_data.at(i));

      //dtv
      head_data.clear();
      head_data.append("ch_val");
      head_data.append("name");
      head_data.append("class");
      head_data.append("favorate");
      head_data.append("skip");
      Q_CHECK_PTR(model_dtv);
      model_dtv->setColumnCount(head_data.size());
      count_col_dtv=head_data.size();
      for(int i=0;i<head_data.size();i++)
          model_dtv->setHeaderData(i,Qt::Horizontal,head_data.at(i));
  }
  Show_ATV();
  Show_DTV();
  ui->actionWrite_stadard_file->setEnabled(true);
}

//写文件前更新记录表格数据的内容
void MainWindow::Write_File()
{
  qDebug("Write File in MainWindow\n");

  QFile file(write_file_name);
  if(!file.open(QFile::WriteOnly))
  {
      qDebug("Open File form_pc_std.bin Failed\n");
      return ;
  }
  QDataStream out(&file);
  Q_ASSERT(tcl_usb!=NULL);
  tcl_usb->Write_File(out);
  ui->actionWrite_stadard_file->setEnabled(true);
}


void MainWindow::open_file()
{
  read_file_name = QFileDialog::getOpenFileName(
     this,
     "Open Document",
     QDir::currentPath(),
     "Document files (*.bin);;All files(*.*)");
  if (!read_file_name.isNull()) { //用户选择了文件
     // 处理文件
     Read_File();
  } else // 用户取消选择
     QMessageBox::information(this, "Document", "No document", QMessageBox::Ok | QMessageBox::Cancel);
  ui->actionConvert_tv_file->setEnabled(true);
}

QList<QStandardItem *> MainWindow::Get_Item_Row(TCL_MSTAR_ATV_TV* mstar_atv_tv)
{
    Q_CHECK_PTR(mstar_atv_tv!=NULL);
    QList<QStandardItem *> ItemRow;
    QString tmp;
    QList<QString> tmp_str;
    mstar_atv_tv->Get_Data(&tmp_str);
    for(int j=0;j<tmp_str.size();j++)
    {
        tmp=tmp_str.at(j);
      ItemRow<<(new QStandardItem(tmp));
    }
    tmp_str.clear();
    return ItemRow;
}

QList<QStandardItem *> MainWindow::Get_Item_Row(TCL_MTK_ATV_TV* mtk_atv_tv)
{
    Q_CHECK_PTR(mtk_atv_tv);
    QList<QString> tmp_str;
    QList<QStandardItem *>ItemRow;
    mtk_atv_tv->Get_Data(&tmp_str);
    QString tmp;
    for(int j=0;j<tmp_str.size();j++)
    {
         tmp=tmp_str.at(j);
         ItemRow<<(new QStandardItem(tmp));
    }
    return ItemRow;

}

QList<QStandardItem *> MainWindow::Get_Item_Row(TCL_MSTAR_DTV_TV *mstar_dtv_tv)
{
    Q_CHECK_PTR(mstar_dtv_tv);
    QList<QString> tmp_str;
    QList<QStandardItem *> ItemRow;
    mstar_dtv_tv->Get_Data(&tmp_str);
    for(int j=0;j<tmp_str.size();j++)
    {
        ItemRow<<(new QStandardItem(tmp_str.at(j)));
    }
    tmp_str.clear();
    return ItemRow;
}

QList<QStandardItem *> MainWindow::Get_Item_Row(TCL_MTK_DTV_TV *mtk_dtv_tv)
{

    Q_CHECK_PTR(mtk_dtv_tv);
    QList<QString>tmp_str;
    QString tmp;
    QList<QStandardItem *> ItemRow;
    tmp.setNum(mtk_dtv_tv->ui4_ch_val);
    tmp_str.append(tmp);

    tmp=mtk_dtv_tv->ps_name;
    tmp_str.append(tmp);
    tmp.setNum(mtk_dtv_tv->ui2_tv);
    tmp_str.append(tmp);
    tmp.setNum(mtk_dtv_tv->ui2_isfav);
    tmp_str.append(tmp);
    tmp.setNum(mtk_dtv_tv->ui2_isskip);
    tmp_str.append(tmp);



    for(int j=0;j<tmp_str.size();j++)
    {
        tmp=tmp_str.at(j);
        ItemRow<<(new QStandardItem(tmp));
     }
    tmp_str.clear();
    return ItemRow;
}

void MainWindow::Show_ATV()
{

      QList<QStandardItem*> ItemRow;
      QString Solution;
    //  QString TextShow="当前显示ATV信息，请点击单元格直接修改内容,修改完毕后请点击编辑->保存ATV,否则当前修改将丢失";
      modify=false;//
      Q_CHECK_PTR(tcl_usb);
      int count_row_atv=0;
      if(tcl_usb->q_mtk_atv_tv.size()!=0)
      {
          Solution=QString("MTK_ATV");
          TCL_MTK_ATV_TV *mtk_atv_tv;
          count_row_atv=tcl_usb->q_mtk_atv_tv.size();//记录行数
          int i=0;
          for(i=cur_atv_loc;i<tcl_usb->q_mtk_atv_tv.size()&&(i-cur_atv_loc)<SHOW_NUM_DATA;i++)
          {
                      mtk_atv_tv=tcl_usb->q_mtk_atv_tv.at(i);
                      ItemRow=Get_Item_Row(mtk_atv_tv);
                      //count_row_atv++;
                      model_atv->appendRow(ItemRow);
                      ItemRow.clear();

          }
          if(i>=tcl_usb->q_mtk_atv_tv.size())
              ui->apB_NextPage->setEnabled(false);
          else
              ui->apB_NextPage->setEnabled(false);

      }else if(tcl_usb->q_mstar_atv_tv.size()!=0)
      {


         Solution="MSTAR-ATV";
        TCL_MSTAR_ATV_TV *mstar_atv_tv;
        count_row_atv=0;
        int i=0;
         for(i=cur_atv_loc;i<tcl_usb->q_mstar_atv_tv.size()&&count_row_atv<SHOW_NUM_DATA;i++)
             {

             if(ms_tv->mstar_atv->ATVProgramData[i].misc.u_ChannelNumber>0
                              &&ms_tv->mstar_atv->ATVProgramData[i].ui2_wPLL>0
                              &&ms_tv->mstar_atv->ATVProgramData[i].misc.u_Hide==0
                )
                {
                      if(i<tcl_usb->q_mstar_atv_tv.size())
                      mstar_atv_tv=tcl_usb->q_mstar_atv_tv.at(i);
                      else
                          break;
                      ItemRow=Get_Item_Row(mstar_atv_tv);
                      count_row_atv++;
                      model_atv->appendRow(ItemRow);
                      ItemRow.clear();
                      mstar_atv_tv=NULL;
                  }
          }
         cur_atv_loc=i;
         if(i>=tcl_usb->q_mstar_atv_tv.size())
             ui->apB_NextPage->setEnabled(false);
         else
             ui->apB_NextPage->setEnabled(true);

      }else{
          qDebug("In Show ATV error\n");
      }

      ui->tableView_atv->setModel(model_atv);

      for(int i=0;i<count_row_atv;i++)
      {
          for(int j=0;j<count_col_atv;j++)
          {
               model_atv->item(i,j)->setTextAlignment(Qt::AlignCenter);
           if(i%2==0)
               model_atv->item(i,j)->setBackground(QBrush(QColor(0,191,255)));
           else
             model_atv->item(i,j)->setBackground(QBrush(QColor(135,206,235)));
          }
      }
      if(cur_atv_loc<SHOW_NUM_DATA)
        ui->apB_LastPage->setEnabled(false);
      if(cur_atv_loc>=SHOW_NUM_DATA)
          ui->apB_LastPage->setEnabled(false);


      modify=true;
}

    void MainWindow::Show_DTV()
    {
    ///
      int count_row_dtv=0;
      QList<QStandardItem*> ItemRow;
      modify=false;
      Q_CHECK_PTR(tcl_usb);
      if(tcl_usb->q_mtk_dtv_tv.size()!=0)
      {
          TCL_MTK_DTV_TV *mtk_dtv_tv;
          int i=0;
          for(i=cur_dtv_loc;i<tcl_usb->q_mtk_dtv_tv.size()&&count_row_dtv<SHOW_NUM_DATA;i++)
          {
              mtk_dtv_tv=tcl_usb->q_mtk_dtv_tv.at(i);
              Q_CHECK_PTR(mtk_dtv_tv);
              ItemRow=Get_Item_Row(mtk_dtv_tv);
              count_row_dtv++;
              model_atv->appendRow(ItemRow);
              ItemRow.clear();

          }

          if(i>=cur_dtv_loc)
              ui->dpB_NextPage->setEnabled(false);
          else
              ui->dpB_NextPage->setEnabled(true);
      }else if(tcl_usb->q_mstar_dtv_tv.size()!=0)
      {

          TCL_MSTAR_DTV_TV *mstar_dtv_tv;
          count_row_dtv=0;
          int i=0;
          for(i=cur_dtv_loc;i<tcl_usb->q_mstar_dtv_tv.size()&&count_row_dtv<SHOW_NUM_DATA;i++)
          {

              mstar_dtv_tv=tcl_usb->q_mstar_dtv_tv.at(i);
              Q_CHECK_PTR(mstar_dtv_tv);

              count_row_dtv++;
              ItemRow=Get_Item_Row(mstar_dtv_tv);

              model_dtv->appendRow(ItemRow);
              ItemRow.clear();
          }
          if(i>=tcl_usb->q_mstar_dtv_tv.size())
              ui->dpB_NextPage->setEnabled(false);
          else
              ui->dpB_NextPage->setEnabled(true);

      }else
      {
          qDebug("In Show DTV solution error");
          return ;
      }
      ui->tableView_dtv->setModel(model_dtv);
      for(int i=0;i<count_row_dtv;i++)
          for(int j=0;j<count_col_dtv;j++)
          {
            model_dtv->item(i,j)->setTextAlignment(Qt::AlignCenter);
           if(i%2==0)
           model_dtv->item(i,j)->setBackground(QBrush(QColor(0,191,255)));
           else
             model_dtv->item(i,j)->setBackground(QBrush(QColor(135,206,235)));
          }

     // ui->an_save_DTV->setVisible(true);
      if(cur_dtv_loc<SHOW_NUM_DATA)
          ui->dpB_LastPage->setEnabled(false);
      if(cur_dtv_loc>=SHOW_NUM_DATA)
          ui->dpB_LastPage->setEnabled(true);
      modify=true;
    }

    void MainWindow::Del_Row()
    {
      int list_row=-1;

      if(0==cur_show||2==cur_show)
      {
          is_atv_edit=true;
        //  cur_row=ui->tableView_atv->currentIndex().row();
      }
      else if(1==cur_show||3==cur_show)
      {
          is_dtv_edit=true;
        //  cur_row=ui->tableView_dtv->currentIndex().row();
      }
      else
          qDebug("In MainWindow::Del_Row() cur_show<0 or cur_show >3");


      list_row=Find_List_Row();
      if(0==cur_show||2==cur_show)
      {
        model_atv->removeRow(cur_row);
      }
      else
      {
        model_dtv->removeRow(cur_row);
      }

      //According to cur_show value:0-mtk_atv,1-mtk_dtv,2-mstar_atv,3-mstar_dtv
      Q_ASSERT(tcl_usb!=NULL);
    switch(cur_show)
    {
    case 0:
        {
          tcl_usb->q_mtk_atv_tv.removeAt(list_row);
          //mt_tv->
          break;
        }
    case 1:
        {
          tcl_usb->q_mtk_dtv_tv.removeAt(list_row);
          //mt_tv->
          break;
         }
    case 2:
        {
          tcl_usb->Del_ATV_Row(list_row);
          ms_tv->Del_ATV_Row(list_row);//直接将相应行置0
          break;
          }
    case 3:
        {
          tcl_usb->q_mstar_dtv_tv.removeAt(list_row);
          ms_tv->Del_DTV_Row(list_row);//删去一行，同时更新dtvb或dtvbc
          break;
          }

    default:
        qDebug("In DelRow cur_show error");

    }
    }

    void MainWindow::set_cur_row(QModelIndex index)
    {
        cur_row=index.row()>0?index.row():0;
    }

    void MainWindow::set_cur_show(int currentIndex)
    {
        Q_CHECK_PTR(tcl_usb);
        if(0==currentIndex&&0==tcl_usb->Get_Solution())
            cur_show=0;
        else if(0==currentIndex&&1==tcl_usb->Get_Solution())
            cur_show=2;
        else if(1==currentIndex&&0==tcl_usb->Get_Solution())
            cur_show=1;
        else if(1==currentIndex&&1==tcl_usb->Get_Solution())
            cur_show=3;
        else
            qDebug("In MainWindow::set_cur_show cur_show error");
    }

    void MainWindow::Show_Menu(QPoint)
    {
      if(cmenu)
      {
          delete  cmenu;
          cmenu=NULL;
      }
      if(cur_show==0||cur_show==2)
      {
          cmenu=new QMenu(ui->tableView_atv);
      }
      else
      {
          cmenu=new QMenu(ui->tableView_dtv);
      }


      QAction *an_modify=cmenu->addAction("修改");
      QAction *an_delrow=cmenu->addAction("删除");


      connect(an_modify,SIGNAL(triggered()),this,SLOT(Md_Data()));
      connect(an_delrow,SIGNAL(triggered()),this,SLOT(Del_Row()));

      cmenu->exec(QCursor::pos());

    }

//row is the data at table location.
    void MainWindow::Md_Data()
    {

        int list_row=-1;
        switch(cur_show)
        {
        case 2:
        {
            list_row=Find_List_Row();
            //Q_ASSERT(tcl_usb!=NULL);
            Q_ASSERT(tcl_usb->q_mstar_atv_tv.size()>=list_row);
            TCL_MSTAR_ATV_TV *mstar_atv=tcl_usb->q_mstar_atv_tv.at(list_row);
            Q_CHECK_PTR(mstar_atv);
            Md_atv *modify=new Md_atv(mstar_atv);
            Q_CHECK_PTR(modify);
            modify->show();
            connect(modify,SIGNAL(Mstar_Close(TCL_MSTAR_ATV_TV)),this,SLOT(Do_Md(TCL_MSTAR_ATV_TV)));
            break;
        }
        case 3:
        {
            list_row=Find_List_Row();
            Q_ASSERT(tcl_usb->q_mstar_dtv_tv.size()>=list_row);
            TCL_MSTAR_DTV_TV *mstar_dtv=tcl_usb->q_mstar_dtv_tv.at(list_row);
            Q_CHECK_PTR(mstar_dtv);
            Md_dtv *modify=new Md_dtv(mstar_dtv);
            Q_CHECK_PTR(modify);
            modify->show();

            connect(modify,SIGNAL(Mstar_Close(TCL_MSTAR_DTV_TV)),this,SLOT(Do_Md(TCL_MSTAR_DTV_TV)));

            break;
        }
        case 0:
        {
            list_row=Find_List_Row();
            Q_ASSERT(tcl_usb->q_mtk_atv_tv.size()>=list_row);
            TCL_MTK_ATV_TV *mtk_atv=tcl_usb->q_mtk_atv_tv.at(list_row);
            Q_CHECK_PTR(mtk_atv);
            Md_atv *modify=new Md_atv(mtk_atv);
            Q_CHECK_PTR(modify);
            modify->show();

            connect(modify,SIGNAL(Mtk_Close(TCL_MTK_ATV_TV)),this,SLOT(Do_Md(TCL_MTK_ATV_TV)));

            break;
        }
        case 1:
        {
            list_row=Find_List_Row();
            Q_ASSERT(tcl_usb->q_mtk_dtv_tv.size()>=list_row);
            TCL_MTK_DTV_TV *mtk_dtv=tcl_usb->q_mtk_dtv_tv.at(list_row);
            Q_CHECK_PTR(mtk_dtv);
            Md_dtv *modify=new Md_dtv(mtk_dtv);
            Q_CHECK_PTR(modify);
            modify->show();

            connect(modify,SIGNAL(Mtk_Close(TCL_MTK_DTV_TV)),this,SLOT(Do_Md(TCL_MTK_DTV_TV)));
            break;
        }
        default:
            Q_ASSERT_X(cur_show>=0&&cur_show<4,"In MainWindow::Md_Data() Line 907","cur_show error");
            //qDebug("In MainWindow::Md_Data() cur_show error!");
         }


    }

    void MainWindow::Do_Md(TCL_MSTAR_ATV_TV ms_atv)
    {
        int qlist_row=Find_List_Row();
        Q_CHECK_PTR(model_atv);
        model_atv->removeRow(cur_row);
        QList<QStandardItem *> ItemRow=Get_Item_Row(&ms_atv);
        model_atv->insertRow(cur_row,ItemRow);
        for(int i=0;i<ms_atv.Count_Item();i++)
        model_atv->item(cur_row,i)->setTextAlignment(Qt::AlignCenter);

       // tcl_usb->q_mstar_atv_tv.removeAt(qlist_row);
       // tcl_usb->q_mstar_atv_tv.insert(qlist_row,&ms_atv);
        Q_CHECK_PTR(ms_tv);
        for(int i=0;i<ms_atv.Count_Item();i++)
           ms_tv->Mod_ATV(qlist_row,i,ms_atv.Get_Item(i));

    }
    void MainWindow::Do_Md(TCL_MSTAR_DTV_TV ms_dtv)
    {
        int qlist_row=Find_List_Row();
        Q_CHECK_PTR(model_dtv);
        model_dtv->removeRow(cur_row);
        QList<QStandardItem *> ItemRow=Get_Item_Row(&ms_dtv);
        model_dtv->insertRow(cur_row,ItemRow);
        for(int i=0;i<ms_dtv.Count_Item();i++)
        model_dtv->item(cur_row,i)->setTextAlignment(Qt::AlignCenter);

       // tcl_usb->q_mstar_atv_tv.removeAt(qlist_row);
       // tcl_usb->q_mstar_atv_tv.insert(qlist_row,&ms_atv);
        Q_CHECK_PTR(ms_tv);
        for(int i=0;i<ms_dtv.Count_Item();i++)
           ms_tv->Mod_DTV(qlist_row,i,ms_dtv.Get_Item(i));
    }

    void MainWindow::Do_Md(TCL_MTK_ATV_TV mt_atv)
    {
        int qlist_row=Find_List_Row();
        Q_CHECK_PTR(model_atv);
        model_atv->removeRow(cur_row);
        QList<QStandardItem *> ItemRow=Get_Item_Row(&mt_atv);
        model_atv->insertRow(cur_row,ItemRow);
        for(int i=0;i<mt_atv.Count_Item();i++)
            model_atv->item(cur_row,i)->setTextAlignment(Qt::AlignCenter);

        Q_CHECK_PTR(ms_tv);
        for(int i=0;i<mt_atv.Count_Item();i++)
        {
            ms_tv->Mod_ATV(qlist_row,i,mt_atv.Get_Item(i));
        }

    }

    void MainWindow::Do_Md(TCL_MTK_DTV_TV mt_dtv)
    {
        int qlist_row=Find_List_Row();
        Q_CHECK_PTR(model_dtv);
        model_dtv->removeRow(cur_row);
        QList<QStandardItem *> ItemRow=Get_Item_Row(&mt_dtv);
        model_dtv->insertRow(cur_row,ItemRow);
        for(int i=0;i<mt_dtv.Count_Item();i++)
            model_dtv->item(cur_row,i)->setTextAlignment(Qt::AlignCenter);

        Q_CHECK_PTR(ms_tv);
        for(int i=0;i<mt_dtv.Count_Item();i++)
        {
            ms_tv->Mod_DTV(qlist_row,i,mt_dtv.Get_Item(i));
        }

    }

    void MainWindow::Mod_ATV_std()
    {

    }
    void MainWindow::Mod_DTV_std()
    {

    }

/*
    void MainWindow::DataChanged(QStandardItem *item)
    {
      //qDebug("Item Changed");
      if(modify)
      {
          //handles modify
          int row=Find_List_Row(item->row());
          int col=item->column();

          switch(cur_show)
          {
          case 0:
              {
                tcl_usb->q_mtk_atv_tv.at(row)->set_Data(col,item->data(0));
                break;
              }
          case 1:
              {
                tcl_usb->q_mtk_dtv_tv.at(row)->set_Data(col,item->data(0));

                break;
               }
          case 2:
              {
               // QVariant tmp=item->data(0);
                tcl_usb->q_mstar_atv_tv.at(row)->set_Data(col,item->data(0));
                ms_tv->Mod_ATV(row,col,item->data(0));
                break;
                }
          case 3:
              {
                tcl_usb->q_mstar_dtv_tv.at(row)->set_Data(col,item->data(0));
                ms_tv->Mod_DTV(row,col,item->data(0));
                break;
                }
          }
      }
    }
*/
/*
//click the data the right area show the row datas
void MainWindow::on_DataTable_clicked(QModelIndex index)
{
  ui->ChanText->clear();
  QString cur_str;
  int row=index.row();

  for(int i=0;i<head_data.size();i++)
  {
      cur_str+=head_data.at(i);
      QModelIndex n_index=ui->DataTable->model()->index(row,i);
      QVariant v_tmp=ui->DataTable->model()->data(n_index);
      cur_str+=":";
      cur_str+=v_tmp.toString();
      cur_str+="\n";
  }
  ui->ChanText->setText(cur_str);
}

*/
//file->standard bin
 void MainWindow::Con_std_file()
{
  translate tran;
//for convert files more than one times,I need clear datas last time.
  //if MSTAR
  if(!ms_tv)
      ms_tv=new MSTAR_TV;
  if(!ms_tv)
      qDebug("In MainWindow::Con_std_file new MSTAR_TV error");

  if(!ms_tv->Is_Empty())
  {
      ms_tv->Clear();
  }

  //else MTK

  ms_tv->Read_ATV();
  ms_tv->Read_DTV();
  ms_tv->Empty_Add_One();



  qDebug("In BIN_Convert::%d",ms_tv->mstar_dtv.size());
  tran.convert_bin(ms_tv);
  ui->actionOpen_standard_file->setEnabled(true);
  ui->actionConvert_standard_file->setEnabled(false);

}

//data->file,just write data,edited,to file.
 void MainWindow::Con_tv_file()
{
  translate tran;
  Q_CHECK_PTR(ms_tv);
  tran.bin_modify(ms_tv);

  ms_tv->Clear();
  ms_tv->Empty_Dul_One();
  model_atv->clear();
  model_dtv->clear();
  ui->actionConvert_standard_file->setEnabled(true);
  ui->actionConvert_tv_file->setEnabled(false);
}

 void MainWindow::NextPage()
{
    if(0==cur_show||2==cur_show)
    {
        cur_atv_loc+=SHOW_NUM_DATA;
        model_atv->clear();
        Show_ATV();
    }else if(1==cur_show||3==cur_show)
    {
        cur_dtv_loc+=SHOW_NUM_DATA;
        model_dtv->clear();
        Show_DTV();
    }else
        qDebug("In MainWindow::NextPage() error");
}
 void MainWindow::LastPage()
{
    if(0==cur_show||2==cur_show)
    {
        cur_atv_loc-=SHOW_NUM_DATA;
        model_atv->clear();
        Show_ATV();
    }else if(1==cur_show||3==cur_show)
    {
        cur_dtv_loc-=SHOW_NUM_DATA;
        model_dtv->clear();
        Show_DTV();
    }else
        qDebug("In MainWindow::LastPage() error");
}

 bool MainWindow::winEventFilter(MSG *msg)
{
  int msgType = msg->message;
  qDebug("1");
  if(msgType == WM_DEVICECHANGE)
  {
      qDebug() << "winEvent in MgFrame : WM_DEVICECHANGE" ;
      PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
      switch(msg->wParam)
      {
      case DBT_DEVICEARRIVAL:
          if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
          {
              PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
              if (lpdbv -> dbcv_flags == 0)
              {
                  // 插入U盘，此处可以做你想做的事
                  qDebug("fuckfuckfuck");
                  QString USBDisk = QString(this->FirstDriveFromMask(lpdbv ->dbcv_unitmask));
                  qDebug() << "USB_Arrived and The USBDisk is: "<<USBDisk ;
              }
          }
          break;
                 case DBT_DEVICEREMOVECOMPLETE:
          if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
          {
              PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
              if (lpdbv -> dbcv_flags == 0)
              {
                  // 拔出U盘，此处可以做你想做的事
                  qDebug() << "USB_Removed";
              }
          }
          break;
      }
  }
  return false;
}

char MainWindow::FirstDriveFromMask (ULONG unitmask)
{
  char i;
  for (i = 0; i < 26; ++i)
  {
      if (unitmask & 0x1)
          break;
      unitmask = unitmask >> 1;
  }
  return (i + 'A');
}
